home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9204.ARJ / 1004046A < prev    next >
Text File  |  1992-06-02  |  29KB  |  625 lines

  1. /* 
  2. menu =  controls cursor movement and menu display and selection -
  3.         returns the number of the selected menu item.              
  4.  
  5.         values:                 a string consisting of two digits showing
  6.                                 the number of menu choices, followed by  
  7.                                 four digits for each choice, the first   
  8.                                 two showing the starting column of the  
  9.                                 menu item and the second two its length. 
  10.                                                                         
  11.         menu_first_line:        the first line of instructions in the    
  12.                                 menu mode.                               
  13.                                                                         
  14.         menu_second_line:       the second line in the menu mode. It con-
  15.                                 tains the menu choices.                  
  16.                                                                         
  17.         screen_first_line:      the first line of instructions in the   
  18.                                 screen mode.                             
  19.                                                                         
  20.         screen_second_line:     the second line of instructions in the   
  21.                                 screen mode.                             
  22.                                                                         
  23.         menu_type:              0: Start with Menu Mode.                 
  24.                                    Alphanumeric characters are ignored   
  25.                                    when entered in screen mode.          
  26.                                 1: Start with Menu Mode.                 
  27.                                    Alphanumeric characters are displayed
  28.                                    when entered in screen mode
  29.                                 2: Start with Screen Mode.               
  30.                                    Alphanumeric characters are ignored   
  31.                                    when entered in screen mode.          
  32.                                 3: Start with Screen Mode.               
  33.                                    Alphanumeric characters are displayed 
  34.                                    when entered in screen mode.          
  35.                                                                         
  36.         screen_color:           color for screen mode.                   
  37.         menu_color:             color for menu display.
  38.                                                                         
  39.         highlight_color:        color of selected menu item              
  40.                                                                         
  41.         select_no:              number of menu items using selection     
  42.                                                                         
  43.         escape_char:            number of the key selected for escape    
  44.                                 from the screen display.                 
  45.                                                                         
  46.         map:                    bit map of permitted cursor locations.   
  47.                                 The cursor will go to permitted locations
  48.                                 only and no others.                      
  49. */
  50.  
  51. int menu(char values[],char menu_first_line[],char menu_second_line[],
  52.      char screen_first_line[],char screen_second_line[],int menu_type,
  53.      int screen_color,int menu_color,int highlight_color, int select_no,
  54.      int escape_char, int first_line_loc, char map[25][10])
  55. {
  56.         union REGS reg;
  57.         int i,choices,indx,start,length,menu_second_line_length;
  58.         int interim,remainder,temp;
  59.         char spaces[80],prev_char;
  60.  
  61.         for (i=0; i<76; i++)
  62.                 spaces[i] = ' ';
  63.         spaces[76] = '\0';
  64.         menu_second_line_length = strlen(menu_second_line);
  65.         gotoxy(2,first_line_loc);
  66.         choice = 1;
  67.         if (menu_type <= 1)
  68.         {
  69.                 color_printf("%s",menu_color,menu_first_line);
  70.                 gotoxy(2,first_line_loc+1);
  71.                 length = values[4] - '0';
  72.                 length = 10 * length + values[5] - '0';
  73.                 for (indx = 0; indx < menu_second_line_length; indx++)
  74.                         if (indx < length)
  75.                                 putcolorchar(menu_second_line[indx],
  76.                                         highlight_color);
  77.                         else
  78.                                 putcolorchar(menu_second_line[indx],
  79.                                         menu_color);
  80.         }
  81.         else
  82.         {
  83.                 color_printf(screen_first_line,menu_color);
  84.                 gotoxy(2,first_line_loc+1);
  85.                 color_printf(screen_second_line,menu_color);
  86.         }
  87.         choices = 10 * (values[0] - '0') + values[1] -'0';
  88.         gotoxy(column,row);
  89.         for(;;)
  90.         {
  91.                 key_id = getch();
  92.                 if (key_id == 0)
  93.                         key_id = getch()+256;
  94.                 if (menu_type <= 1)
  95.                 {
  96.                         switch(key_id)
  97.                         {
  98.                         case 13:
  99.                                 if(choice > select_no)
  100.                                         goto ExitPoint;
  101.                                 change_line_color(47);
  102.                                 gotoxy(2,23);
  103.                                 color_printf("%s",screen_color,spaces);
  104.                                 gotoxy(2,23);
  105.                                 color_printf("%s",menu_color,
  106.                                 screen_first_line);
  107.                                 gotoxy(2,24);
  108.                                 color_printf("%s",screen_color,spaces);
  109.                                 gotoxy(2,24);
  110.                                 color_printf("%s",menu_color,
  111.                                         screen_second_line);
  112.                                 gotoxy(column,row);
  113.                                 menu_type +=2;
  114.                                 break;
  115.                         case 333:    /*Right Arrow*/
  116.                                 choice = choice + 2;
  117.                         case 331:  /*Left Arrowb*/
  118.                                 --choice;
  119.                                 if (choice < 1)
  120.                                         choice = choices;
  121.                                 if (choice > choices)
  122.                                         choice = 1;
  123.                                 start = 10 * (values[(choice-1)*4+2] - '0')
  124.                                         +values[(choice-1)*4+3] - '0';
  125.                                 length = 10 * (values[(choice-1)*4+4] - '0')
  126.                                         +values[(choice-1)*4+5] - '0';
  127.                                 gotoxy(2,24);
  128.                                 for (indx = 0;indx < menu_second_line_length;
  129.                                         indx++)
  130.                                 {
  131.                                         if ((indx >= start) && (indx < start
  132.                                         + length))
  133.                                                 putcolorchar
  134.                                                         (menu_second_line
  135.                                                         [indx],
  136.                                                         highlight_color);
  137.                                         else
  138.                                                 putcolorchar
  139.                                                         (menu_second_line
  140.                                                         [indx],
  141.                                                         menu_color);
  142.                                 }
  143.                                 gotoxy(column,row);
  144.                                 break;
  145.                         default:
  146.                                 if ((key_id >= 0x41) && (key_id <= 0x7A))
  147.                                 {
  148.                                         temp = toupper(key_id);
  149.                                         for (indx=0; indx<=choices; indx++)
  150.                                         {
  151.                                                 start = 10 * (values[
  152.                                                         (indx-1)*4+2] -
  153.                                                         '0')+values[(indx
  154.                                                         -1)*4+3] - '0';
  155.                                                 if (temp == menu_second_line
  156.                                                         [start])
  157.                                                 {
  158.                                                         choice = indx;
  159.                                                         if(choice >
  160.                                                               select_no)
  161.                                                               goto ExitPoint;
  162.                                                         change_line_color
  163.                                                         (47);
  164.                                                         gotoxy(2,23);
  165.                                                         color_printf("%s",
  166.                                                         screen_color,spaces);
  167.                                                         gotoxy(2,23);
  168.                                                         color_printf("%s",
  169.                                                         menu_color,
  170.                                                         screen_first_line);
  171.                                                         gotoxy(2,24);
  172.                                                         color_printf("%s",
  173.                                                         screen_color,spaces);
  174.                                                         gotoxy(2,24);
  175.                                                         color_printf("%s",
  176.                                                         menu_color,
  177.                                                         screen_second_line);
  178.                                                         gotoxy(column,row);
  179.                                                         menu_type += 2;
  180.                                                 }
  181.                                         }
  182.                                 }
  183.                         }
  184.                 }
  185.                 else
  186.                 {
  187.                         if (key_id == escape_char)
  188.                                 goto ExitPoint;
  189.                         switch(key_id)
  190.                         {
  191.                                 case 8:   /*Backspace*/
  192.                                 case 331:  /*Left Arrow*/
  193.                                         if ((menu_type == 0) ||
  194.                                         (menu_type == 2))
  195.                                                 change_line_color
  196.                                                 (screen_color);
  197.                                         do
  198.                                         {
  199.                                                 column--;
  200.                                                 if (column < 0)
  201.                                                 {
  202.                                                         column = 79;
  203.                                                         row --;
  204.                                                         if (row < 0)
  205.                                                                 row = 24;
  206.                                                 }
  207.                                                 indx = column/8;
  208.                                                 remainder = column - indx*8;
  209.                                                 interim = map[row][indx] &
  210.                                                 (0x01 << remainder);
  211.                                         }
  212.                                         while   (interim == 0x00);
  213.                                         gotoxy(column,row);
  214.                                         if ((menu_type == 0) ||
  215.                                         (menu_type == 2))
  216.                                                 change_line_color
  217.                                                 (highlight_color);
  218.                                         break;
  219.  
  220.                                 case 333:  /*Right Arrow*/
  221.                                         if ((menu_type == 0) ||
  222.                                         (menu_type == 2))
  223.                                                 change_line_color
  224.                                                 (screen_color);
  225.                                         do
  226.                                         {
  227.                                                 column++;
  228.                                                 if (column > 79)
  229.                                                 {
  230.                                                         column = 0;
  231.                                                         row ++;
  232.                                                         if (row > 24)
  233.                                                                 row = 0;
  234.                                                 }
  235.                                                 indx = column/8;
  236.                                                 remainder = column - indx*8;
  237.                                                 interim = map[row][indx] &
  238.                                                         (0x01 << remainder);
  239.                                         }
  240.                                         while   (interim == 0x00);
  241.                                         gotoxy(column,row);
  242.                                         if ((menu_type == 0) ||
  243.                                         (menu_type == 2))
  244.                                                 change_line_color
  245.                                                 (highlight_color);
  246.                                         break;
  247.                                 case 13:
  248.                                         column = 0;
  249.                                 case 336:  /*Down Arrow*/
  250.                                         if ((menu_type == 0) ||
  251.                                         (menu_type == 2))
  252.                                                 change_line_color
  253.                                                 (screen_color);
  254.                                         row++;
  255.                                         if (row > 24)
  256.                                                 row = 0;
  257.                                         indx = column/8;
  258.                                         remainder = column - indx * 8;
  259.                                         interim = map[row][indx] & (0x01 <<
  260.                                         remainder);
  261.                                         if (interim != 0x00)
  262.                                         {
  263.                                                 gotoxy(column,row);
  264.                                                 if ((menu_type == 0) ||
  265.                                                 (menu_type == 2))
  266.                                                         change_line_color
  267.                                                         (highlight_color);
  268.                                                 break;
  269.                                         }
  270.                                         column = 0;
  271.                                         do
  272.                                         {
  273.                                                 indx = column/8;
  274.                                                 remainder = column - indx*8;
  275.                                                 interim = map[row][indx] &
  276.                                                 (0x01 << remainder);
  277.                                                 if (interim != 0x00)
  278.                                                 {
  279.                                                         gotoxy(column,row);
  280.                                                         if ((menu_type == 0)
  281.                                                         || (menu_type == 2))
  282.                                                            change_line_color
  283.                                                            (highlight_color);
  284.                                                         break;
  285.                                                 }
  286.                                                 column++;
  287.                                                 if (column > 79)
  288.                                                 {
  289.                                                         column = 0;
  290.                                                         row ++;
  291.                                                         if (row > 24)
  292.                                                                 row = 0;
  293.                                                 }
  294.                                         }
  295.                                         while   (interim == 0x00);
  296.                                         break;
  297.                                 case 328:  /*Up Arrow*/
  298.                                         if ((menu_type == 0) ||
  299.                                         (menu_type == 2))
  300.                                                 change_line_color
  301.                                                 (screen_color);
  302.                                         row--;
  303.                                         if (row < 0)
  304.                                                 row = 24;
  305.                                         indx = column/8;
  306.                                         remainder = column - indx * 8;
  307.                                         interim = map[row][indx] & (0x01
  308.                                         << remainder);
  309.                                         if (interim != 0x00)
  310.                                         {
  311.                                                 gotoxy(column,row);
  312.                                                 if ((menu_type == 0) ||
  313.                                                 (menu_type == 2))
  314.                                                         change_line_color
  315.                                                         (highlight_color);
  316.                                                 break;
  317.                                         }
  318.                                         column = 79;
  319.                                         do
  320.                                         {
  321.                                                 indx = column/8;
  322.                                                 remainder = column - indx*8;
  323.                                                 interim = map[row][indx] &
  324.                                                 (0x01 << remainder);
  325.                                                 if (interim != 0x00)
  326.                                                 {
  327.                                                         gotoxy(column,row);
  328.                                                         if ((menu_type == 0)
  329.                                                         || (menu_type == 2))
  330.                                                            change_line_color
  331.                                                            (highlight_color);
  332.                                                         break;
  333.                                                 }
  334.                                                 column--;
  335.                                                 if (column < 0)
  336.                                                 {
  337.                                                         column = 79;
  338.                                                         row --;
  339.                                                         if (row < 0)
  340.                                                                 row = 24;
  341.                                                 }
  342.                                         }
  343.                                         while   (interim == 0x00);
  344.                                         break;
  345.                                 default:
  346.                                         if ((menu_type == 1) ||
  347.                                         (menu_type == 3))
  348.                                         {
  349.                                                 putcolorchar(key_id,
  350.                                                 screen_color);
  351.                                                 do
  352.                                                 {
  353.                                                         column++;
  354.                                                         if (column > 79)
  355.                                                         {
  356.                                                                 column = 0;
  357.                                                                 row ++;
  358.                                                                 if (row > 24)
  359.                                                                         row =
  360.                                                                 0;
  361.                                                         }
  362.                                                         indx = column/8;
  363.                                                         remainder = column -
  364.                                                         indx * 8;
  365.                                                         interim = map[row]
  366.                                                         [indx] & (0x01
  367.                                                         << remainder);
  368.                                                 }
  369.                                                 while   (interim == 0x00);
  370.                                                 gotoxy(column,row);
  371.                                 }
  372.                         }
  373.                 }
  374.         }
  375.         ExitPoint:
  376.         return choice;
  377. }
  378.  
  379. /*
  380. change_line_color = changes the color of a line up to a double space
  381. */
  382.  
  383. void change_line_color(int color)
  384. {
  385.         union REGS rin;
  386.         char prev_char;
  387.  
  388.         prev_char = 's';
  389.         for( ; ; )
  390.         {
  391.                 rin.h.ah = 3;
  392.                 rin.h.bh = 0;
  393.                 int86(0x10,&rin,&rin);
  394.                 ch = read_char_from_screen();
  395.                 if((ch == ' ') && (prev_char == ' '))
  396.                         break;
  397.                 prev_char = ch;
  398.                 gotoxy(rin.h.dl,rin.h.dh);
  399.                 putcolorchar(ch,color);
  400.         }
  401.         gotoxy(column,row);
  402. }
  403.  
  404. /*
  405. clearscreen = clears the screen and displays selected color background
  406. */
  407.  
  408. void clearscreen(int color)
  409. {
  410.         int indx;
  411.         union REGS reg;
  412.  
  413.         gotoxy(0,0);
  414.         reg.h.ah = 9;
  415.         reg.h.al = 0x20;
  416.         reg.h.bh = 0;
  417.         reg.h.bl = color;
  418.         reg.x.cx = 2000;
  419.         int86(0x10,®,®);
  420. }
  421.  
  422. /*
  423. color_printf = printf with selected foreground and background colors
  424. */
  425.  
  426. void color_printf (char *msg,int color,...)
  427. {
  428.         union REGS reg;
  429.         char ch, string[2000];
  430.         int i = 0;
  431.         va_list (ap);
  432.  
  433.         va_start (ap,msg);
  434.         vsprintf(string,msg,ap);  /*do printf to string*/
  435.         va_end(ap);
  436.         while ((ch=string[i++]) != '\0')   /*get chars from string till end*/
  437.         {
  438.                 if (ch == 0x0A)   /*is character a line feed*/
  439.                 {
  440.                         reg.h.ah = 3;
  441.                         int86(0x10,®,®);
  442.                                 /*get cursor position*/
  443.                         reg.h.dl = 0;
  444.                         reg.h.dh++;
  445.                                 /*cursor value to beginning of next line*/
  446.                         reg.h.ah = 2;
  447.                         int86(0x10,®,®);
  448.                                 /*set new cursor position*/
  449.                 }
  450.                 else
  451.                 {
  452.                         reg.h.ah = 9;
  453.                         reg.h.al = ch;
  454.                         reg.x.bx = color;
  455.                         reg.x.cx = 1;
  456.                         int86(0x10,®,®);
  457.                                 /*send a color character to display*/
  458.                         reg.h.ah = 3;
  459.                         int86(0x10,®,®);
  460.                                 /*get cursor position in D reg*/
  461.                         reg.x.dx++;
  462.                         reg.h.ah = 2;
  463.                                 /*increment cursor position value*/
  464.                         int86(0x10,®,®);
  465.                                 /*set cursor to new position*/
  466.                 }
  467.         }
  468. }
  469.  
  470.  
  471. /*
  472. gotoxy = moves cursor to selected column and row
  473. */
  474.  
  475. void gotoxy(int col, int row)
  476. {
  477.         union REGS reg;
  478.         reg.h.ah = 2;
  479.         reg.h.bh = 0;
  480.         reg.x.dx = (row << 8) +col;
  481.         int86(0X10,®, ®);
  482. }
  483.  
  484. /*
  485. putcolorchar =  displays a character with selected color foreground
  486.                 and background
  487. */
  488.  
  489. void putcolorchar(char character, int color)
  490. {
  491.         union REGS reg;
  492.         reg.h.ah = 3;
  493.         reg.h.bh = 0;
  494.         int86(0x10,®,®);
  495.         reg.h.ah = 9;
  496.         reg.h.al = character;
  497.         reg.h.bl = color;
  498.         reg.x.cx = 1;
  499.         int86(0x10,®,®);
  500.         reg.h.ah =2;
  501.         reg.h.dl = reg.h.dl+1;
  502.         int86(0x10,®,®);
  503. }
  504.  
  505. /*
  506. read_char_from_screen = reads a character from the screen into 'ch'
  507. */
  508.  
  509. char read_char_from_screen()
  510. {
  511.         char ch;
  512.         union REGS reg;
  513.  
  514.         reg.h.ah = 3;
  515.         reg.h.bh = 0;
  516.         int86(0x10,®,®);
  517.         reg.h.ah = 8;
  518.         int86(0x10,®,®);
  519.         ch = reg.h.al;
  520.         attr = reg.h.ah;
  521.         reg.h.ah =2;
  522.         reg.h.dl = reg.h.dl+1;
  523.         int86(0x10,®,®);
  524.         return ch;
  525. }
  526.  
  527. /*
  528. set_cursor = sets up array of permissible cursor positions
  529. */
  530.  
  531. void set_cursor()
  532. {
  533.         int i,j,indx,remainder,key_value;
  534.         char interim,map[25][10];
  535.  
  536.         FILE *f1;
  537.         f1 = fopen("matrix.c","w");
  538.  
  539.         for(i=0;i<=24;i++)
  540.         {
  541.                 for(j=0;j<=9;j++)
  542.                 {
  543.                         map[i][j]=0x00;
  544.                 }
  545.         }
  546.         row=0;
  547.         column = 0;
  548.         gotoxy(column,row);
  549.         while ((key_value = getch()) != 13)
  550.         {
  551.                 if (key_value == 0)
  552.                         key_value = getch()+128;
  553.                 switch(key_value)
  554.                 {
  555.                         case 8:   /*Backspace*/
  556.                         case 203:  /*Left Arrow*/
  557.                                 --column;
  558.                                 break;
  559.                         case 205:  /*Right Arrow*/
  560.                                 ++column;
  561.                                 break;
  562.                         case 208:  /*Down Arrow*/
  563.                                 ++row;
  564.                                 break;
  565.                         case 200:  /*Up Arrow*/
  566.                                 --row;
  567.                                 break;
  568.                         default:
  569.                                 putch(key_value);
  570.                                 column++;
  571.                         }
  572.                 if (column > 79)
  573.                 {
  574.                         column = 0;
  575.                         row++;
  576.                 }
  577.                 if (column < 0)
  578.                 {
  579.                         column = 0;
  580.                         row--;
  581.                 }
  582.                 if (row < 0)
  583.                         row = 0;
  584.                 gotoxy(column,row);
  585.         }
  586.         row=0;
  587.         column = 0;
  588.         while (row*column < 1896)
  589.         {
  590.                 gotoxy(column,row);
  591.                 ch = read_char_from_screen();
  592.                 if ((ch == 'x') || (ch == 'X'))
  593.                 {
  594.                         indx = column/8;
  595.                         remainder = column - indx * 8;
  596.                         interim = 0x01;
  597.                         interim = interim << remainder;
  598.                         map[row][indx] = map[row][indx] | interim;
  599.                 }
  600.                 column++;
  601.                 if (column > 79)
  602.                 {
  603.                         column = 0;
  604.                         row++;
  605.                 }
  606.                 gotoxy(column,row);
  607.         }
  608.  
  609.         clearscreen(30);
  610.         fputc('{',f1);
  611.         gotoxy(0,0);
  612.         for (i=0;i<=24;i++)
  613.         {
  614.                 for (j=0;j<=9;j++)
  615.                 {
  616.                         fprintf(f1,"0x%x",map[i][j]);
  617.                         if ((i != 24) || (j != 9))
  618.                                 fputc(',',f1);
  619.                 }
  620.                 fprintf(f1,"\n");
  621.         }
  622.         fputc('}',f1);
  623.         fclose(f1);
  624. }
  625.